﻿#region

using System.Collections.Generic;
using MediaSage.Lib.Configuration;
using MediaSage.Lib.Database;
using MediaSage.Lib.Utilities;

#endregion

namespace MediaSage.Lib.Plugin
{
    #region Enumerations

    /// <summary>
    ///     Defines the type of the ID field for a plugin
    /// </summary>
    public enum PluginIDObjectType
    {
        integer,
        str,
        file
    }

    /// <summary>
    ///     Defines the type of provider
    /// </summary>
    public enum PluginType
    {
        file, // Plugins that provide file information, like Filename to Series translation, used in figuring out what a file is
        metadata, // Plugins that provide metadata info
        UI, // Plugins that provide UI services for the MediaCenter App
        maintenance, // Plugins that run on a regular basis to do larger tasks
        optimization, // Plugins that run more regularly to do optimization tasks
        metadatawriter // Plugins that export metadata from media other programs to read (Example: dvd_id.xml)
    }

    #endregion Enumerations

    /// <summary>
    ///     Abstract class defining the interface between MediaSage and all metadata providers
    /// </summary>
    public abstract class PluginBase
    {
        #region Fields

        protected Config config;
        protected Database.Database database;

        /// <summary>
        /// By default images will not be downloaded
        /// </summary>
        protected bool downloadImages = false;

        protected Logger logger;

        /// <summary>
        /// Database construct holding plugin configuration
        /// </summary>
        protected PluginEntity pluginEntity;

        #endregion Fields

        #region Constructors

        /*
        protected PluginBase(MediaSage.Lib.Configuration.Config Configuration)
        {
            this.config = Configuration;
            this.logger = Configuration.Logger;
            logger.Log("Plugn Initialized");
            this.database = new MediaSage.Lib.Database.Database(Configuration);

            // Now try to load the configuration
            VersionInfo vi = GetVersionInfo();
            this.pluginEntity = Utilities.Plugin.GetConfigurationByName(vi.name, vi.majorVersion, vi.minorVersion, vi.buildNumber);
            if (this.pluginEntity == null)
            {
                logger.Log("Provider: " + vi.name + " has no configuration in the database");
                this.pluginEntity = new PluginEntity();
            }
            logger.Log("Plugin: " + vi.name + " initialized");
        }
        */

        protected PluginBase(Config Configuration, PluginEntity PE)
        {
            config = Configuration;
            logger = Configuration.Logger;
            pluginEntity = PE;
            logger.Log("Plugn Initialized");
            database = new Database.Database(Configuration);

            // Now try to load the configuration
            if (pluginEntity == null)
            {
                VersionInfo vi = GetVersionInfo();
                pluginEntity = Utilities.Plugin.GetConfigurationByNameAndVer(vi.Name, vi.MajorVersion, vi.MinorVersion,
                                                                             vi.BuildNumber);
                if (pluginEntity == null)
                {
                    logger.Log("Provider: " + vi.Name + " has no configuration in the database");
                    pluginEntity = new PluginEntity();
                }
            }
            logger.Log("Plugin: " + pluginEntity.Name + " initialized");
        }

        #endregion Constructors

        #region Methods

        public void EnableImageDownloads(bool value)
        {
            downloadImages = value;
        }

        public abstract string[] About();

        // Called by MediaSage when installing the plugin to define which extensions are handled by the plugin
        public abstract IList<MediaFileTypesEntity> ExtensionsHandled();

        public abstract PluginEntity GetConfiguration();

        public PluginIDObjectType GetIDType()
        {
            return pluginEntity.IDObjectType;
        }

        public abstract MediaClass GetMedia(MediaClass inputMedia, MediaMergeType mergeType);

        public abstract MediaClass GetMediaByID(object ID);

        public abstract MediaClass GetUpdateForMedia(MediaClass inputMedia, MediaMergeType MergeType);

        public abstract VersionInfo GetVersionInfo();

        // Used for Maintenance and Optimization tasks
        public abstract bool Run();

        // Used for Writer Tasks
        public abstract bool Run(MediaClass mediaObject);

        public abstract List<MediaSearchReturn> SearchForMedia(MediaClass mediaObject);

        public abstract List<MediaSearchReturn> SearchForMedia(MediaClass mediaObject, int maxReturns);

        public abstract List<MediaSearchReturn> SearchForMediaByName(string name);

        public abstract List<MediaSearchReturn> SearchForMediaByName(string name, int maxReturns);

        public abstract List<MediaSearchReturn> SearchForMediaByNameAndYear(string name, int year);

        public abstract List<MediaSearchReturn> SearchForMediaByNameAndYear(string name, int year, int maxReturns);

        public abstract void SetDefaultConfig();

        public abstract IList<PersonEntity> SearchPersonByName(string name);

        #endregion Methods
    }
}